package com.javaee.ch11.service;

import com.javaee.ch11.common.exceptions.ServiceException;
import com.javaee.ch11.entity.Department;
import com.javaee.ch11.entity.Reader;
import com.javaee.ch11.mapper.DepartmentMapper;
import com.javaee.ch11.web.dto.DepartmentQueryDto;
import com.javaee.ch11.web.dto.ReaderQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentService {

    @Autowired
    public  DepartmentMapper departmentMapper;

    public List<Department> listAll() {
        return departmentMapper.selectAll();
    }

    public Department getById(Long id) {
        return departmentMapper.selectById(id);
    }

    public void add(Department department) {
        if (departmentMapper.insert(department) != 1) {
            throw new ServiceException("部门信息添加失败");
        }
    }

    public void updateById(Department department) {
        if (departmentMapper.updateById(department) != 1) {
            throw new ServiceException("部门信息更新失败");
        }
    }

    public void deleteById(Long id) {
        if (departmentMapper.deleteById(id) != 1) {
            throw new ServiceException("部门信息删除失败");
        }
    }

    public List<Department> queryDepartment(DepartmentQueryDto queryDto) {
        return departmentMapper.queryDepartments(queryDto);
    }
}    